<template>
  <div>
    <el-dialog
      title="新增常用汇率"
      v-model="props.newUsualRatesVisible"
      width="600px"
      @close="newUsualRatesCancel"
    >
      <el-form
        :model="newUsualRatesForm"
        ref="newUsualRatesFormRef"
        :rules="manualSettingFormRules"
      >
        <el-form-item label="货币" prop="code" required>
          <el-select
            style="width: 100%"
            v-model="newUsualRatesForm.code"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="dict in sys_currency_code"
              :key="dict.id"
              :label="dict.description"
              :value="dict.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="seq" required>
          <el-input-number
            v-model="newUsualRatesForm.seq"
            placeholder="请输入"
            style="width: 100%"
            :min="0"
          />
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <el-button @click="newUsualRatesCancel">取 消</el-button>
        <el-button type="primary" @click="handleFormSubmit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup >
import { reactive, ref, onMounted } from "vue";
const { proxy } = getCurrentInstance();
const emits = defineEmits(["newUsualRatesCancel", "handleFormSubmit"]);
const sys_currency_code = ref([]); //货币
const props = defineProps({
  newUsualRatesVisible: {
    type: Boolean,
    default: false,
  },
  item: {
    type: Object,
    default: {},
  },
});
watch(
  () => props.newUsualRatesVisible,
  () => {
    if (!props.newUsualRatesVisible) return;
    // 获取货币
    product.currency.currencyList().then((res) => {
      sys_currency_code.value = res.data;
    });
  }
);
const newUsualRatesForm = ref({
  code: null,
  seq: 0,
});
const manualSettingFormRules = ref({
  code: [{ required: true, message: "请选择", trigger: "blur" }],
  seq: [
    { required: true, message: "排序不能为空", trigger: "blur" },
    {
      pattern: /^-?\d+$/,
      message: "只能输入整数",
      trigger: "blur",
    },
  ],
});

const handleFormSubmit = () => {
  proxy.$refs["newUsualRatesFormRef"].validate((valid) => {
    if (!valid) return;
    rate.exchangerate
      .exchangerateAddcommonlyusedcurrency({
        data: newUsualRatesForm.value,
      })
      .then((response) => {
        if (response.code === 200) {
          emits("handleFormSubmit");
          proxy.resetForm("newUsualRatesFormRef");
          proxy.$modal.msgSuccess("新增汇率成功");
        }
      });
  });
};
const newUsualRatesCancel = () => {
  emits("newUsualRatesCancel");
  proxy.resetForm("newUsualRatesFormRef");
};
</script>